# coding=utf-8

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import sys
import datetime

def sendMail(mailto_list, sub, content):
    reload(sys)
    sys.setdefaultencoding("utf-8")
    # mailto_list = ["cbi28@qq.com"]    # 邮件接收方的邮件地址数组
    mail_host = "smtp.163.com"    # 邮件传送服务器，此处为QQ
    mail_user = "15068749417"    # 邮件发送方的邮箱账号
    mail_pass = "Iam341039"    # 邮件发送方的邮箱密码，此为授权码
    mail_postfix = "163.com"

    # mailto_list = ["cbi28@qq.com"]
    # mail_host = "smtp.exmail.qq.com"
    # mail_user = "1297433543@qq.com"
    # mail_pass = "htpjkfcbeggpijbd"

    me = "小权" + "<" + mail_user + "@" + mail_postfix+">"    # 此处为163邮箱的
    # me = "is or no" + "<" + mail_user + ">"    # 此处为腾讯邮箱的
    msg = MIMEText(content, _subtype="plain", _charset="utf-8")
    msg["Subject"] = sub    # 邮件主题
    msg["From"] = me
    msg["To"] = ";".join(mailto_list)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user, mail_pass)
        server.sendmail(me, mailto_list, msg.as_string())
        server.close()
        return True
    except Exception, e:
        print str(e)
        return False

def main():
    mailto_list = ["cbi28@qq.com"]
    sub = u"网站报错信息"
    content = u"网站报错信息"
    if sendMail(mailto_list, sub, content):
        print u"发送成功"
    else:
        print u"发送失败"

if __name__ == '__main__':
    main()
